package com.npf.imgserver.config.init;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.google.common.collect.Maps;
import com.npf.imgserver.common.constant.Constants;
import com.npf.imgserver.dao.CustomerMapper;
import com.npf.imgserver.entity.Customer;
import com.npf.imgserver.utils.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.List;
import java.util.Map;

/**
 * Created with IDEA
 * 初始化加载 customer信息到Redis
 * 加载顺序 @Autowired > @PostConstruct > @Construct
 */
@Component
public class CustomerListInit {

    @Autowired
    private RedisUtil redisUtil;
    @Autowired
    private CustomerMapper customerMapper;

    @PostConstruct
    public void init() {
        //加载接入商列表到 Redis队列
        List<Customer> customerList = customerMapper.selectList(
                new QueryWrapper<Customer>().orderByDesc("create_date"));
        Map<String, Object> map = Maps.newHashMap();
        customerList.forEach(customer -> {
            //接入商ID 对应接入商信息
            map.put(customer.getAppKey(), customer);
        });
        redisUtil.hmset(Constants.REDIS_CUSTOMER_LIST, map);
    }
}
